import java.util.*;

/**
 * @author LKQ
 * @date 2022/4/17 15:10
 * @description 排序 + 二分查找
 */
public class Solution3 {
    public static void main(String[] args) {

    }
    public int maxProfitAssignment(int[] difficulty, int[] profit, int[] worker) {
        int n = difficulty.length;
        int[][] combine = new int[n][2];
        for (int i = 0; i < n; i++) {
            combine[i][0] = difficulty[i];
            combine[i][1] = profit[i];
        }
        // 按照难度升序排序
        Arrays.sort(combine, (a, b) -> {
            return a[0] - b[0];
        });
        int ans = 0;
        for (int skill : worker) {
            int l = 0, r = n - 1, best = 0;
            while (l <= r) {
                int mid = (l + r) / 2;
                if (skill >= combine[mid][0]) {
                    best = Math.max(best, combine[mid][1]);
                    l = mid + 1;
                }else {
                    r = mid - 1;
                }
            }
            ans += best;
        }
        return ans;
    }
}
